<!doctype html><html><head>
    <title>A save and restore canvas state example</title>

<script src="../canvas.vml.js"></script>
<script src="../canvas.js"></script>

<script>
function oncanvasready() {
    drawShape(document.getElementById('vmlcanvas'));
}
function matrix() {
    drawShape(document.getElementById('vmlcanvas'), 1);
}

var globalAlpha = 1.0
function switchAlpha() {
    globalAlpha -= 0.2;
    drawShape(document.getElementById('vmlcanvas'));
}

var lineWidth = 1.0
function switchLineWidth() {
    lineWidth += 2;
    drawShape(document.getElementById('vmlcanvas'));
}

var fontSize = 16;
function switchFontSize() {
    fontSize += 2;
    drawShape(document.getElementById('vmlcanvas'));
}

var shadowBlur = 0;
function switchShadowBlur() {
    shadowBlur += 2;
    drawShape(document.getElementById('vmlcanvas'));
}

function drawShape(canvas, matrix){
    if (!canvas.getContext){ return; }

    var ctx = canvas.getContext('2d');

    ctx.clear();
    if (matrix) {
        ctx.translate(-10, 5);
        ctx.scale(1.2, 0.8);
        ctx.rotate(5 * Math.PI / 180);
    }
    ctx.globalAlpha = globalAlpha;
    ctx.lineWidth = lineWidth;

    ctx.textBaseline = "top";
    ctx.font = fontSize + "pt Arial";
    ctx.shadowColor = "red";
    ctx.shadowOffsetX = 10;
    ctx.shadowOffsetY = 10;
    ctx.shadowBlur = shadowBlur + 10;
    ctx.fillText("red shadow", 80, 20);

    ctx.save();
    ctx.textAlign = "center";
    ctx.font = (fontSize + 4) + "pt 'Arial Black'";
    ctx.fillStyle = "#09F";
    ctx.shadowColor = "green";
    ctx.shadowOffsetX = 5;
    ctx.shadowOffsetY = 5;
    ctx.shadowBlur = shadowBlur + 5;
    ctx.fillText("green shadow", 120, 60);

    ctx.save();
    ctx.textAlign = "end";
    ctx.font = (fontSize + 8) + "pt 'Times New Roman'";
    ctx.globalAlpha = globalAlpha - 0.5;
    ctx.globalCompositeOperation = "source-out";
    ctx.fillStyle = "#eee";
    ctx.shadowColor = "blue";
    ctx.shadowOffsetX = 3;
    ctx.shadowOffsetY = -1;
    ctx.shadowBlur = shadowBlur + 0;
    ctx.fillText("blue shadow", 220, 100);
    ctx.restore();

    ctx.fillText("green shadow", 120, 140);
    ctx.restore();

    ctx.fillText("red shadow", 80, 180);
}
</script>
    <style type="text/css">
      body { margin: 20px; font-family: arial,verdana,helvetica; background: #fff;}
      h1 { font-size: 140%; font-weight:normal; color: #036; border-bottom: 1px solid #ccc; }
      canvas { border: 2px solid #000; float: left; margin-right: 20px; margin-bottom: 20px; }
      pre { float:left; display:block; background: rgb(238,238,238); border: 1px dashed #666; padding: 15px 20px; margin: 0 0 10px 0; }
    </style>
  </head>
  <body>
    <h1>A <code>save</code> and <code>restore</code> canvas state example</h1>
    <div>
      <input type="button" value="matrix effect" onclick="matrix()" />
      <input type="button" value="alpha -0.2" onclick="switchAlpha()" />
      <input type="button" value="lineWidth +2" onclick="switchLineWidth()" />
      <input type="button" value="fontSize +2" onclick="switchFontSize()" />
      <input type="button" value="shadowBlur +2" onclick="switchShadowBlur()" />
      <br />
      <canvas id="vmlcanvas" class="vml" width="300" height="300"></canvas>
      <pre>
&lt;canvas id="vmlcanvas" class="vml" width="300" height="300"&gt;&lt;/canvas&gt;

function oncanvasready() {
    drawShape(document.getElementById('vmlcanvas'));
}
function matrix() {
    drawShape(document.getElementById('vmlcanvas'), 1);
}

var globalAlpha = 1.0
function switchAlpha() {
    globalAlpha -= 0.2;
    drawShape(document.getElementById('vmlcanvas'));
}

var lineWidth = 1.0
function switchLineWidth() {
    lineWidth += 2;
    drawShape(document.getElementById('vmlcanvas'));
}

var fontSize = 16;
function switchFontSize() {
    fontSize += 2;
    drawShape(document.getElementById('vmlcanvas'));
}

var shadowBlur = 0;
function switchShadowBlur() {
    shadowBlur += 2;
    drawShape(document.getElementById('vmlcanvas'));
}

function drawShape(canvas, matrix){
    if (!canvas.getContext){ return; }

    var ctx = canvas.getContext('2d');

    ctx.clear();
    if (matrix) {
        ctx.translate(-10, 5);
        ctx.scale(1.2, 0.8);
        ctx.rotate(5 * Math.PI / 180);
    }
    ctx.globalAlpha = globalAlpha;
    ctx.lineWidth = lineWidth;

    ctx.textBaseline = "top";
    ctx.font = fontSize + "pt Arial";
    ctx.shadowColor = "red";
    ctx.shadowOffsetX = 10;
    ctx.shadowOffsetY = 10;
    ctx.shadowBlur = shadowBlur + 10;
    ctx.fillText("red shadow", 80, 20);

    ctx.save();
    ctx.textAlign = "center";
    ctx.font = (fontSize + 4) + "pt 'Arial Black'";
    ctx.fillStyle = "#09F";
    ctx.shadowColor = "green";
    ctx.shadowOffsetX = 5;
    ctx.shadowOffsetY = 5;
    ctx.shadowBlur = shadowBlur + 5;
    ctx.fillText("green shadow", 120, 60);

    ctx.save();
    ctx.textAlign = "end";
    ctx.font = (fontSize + 8) + "pt 'Times New Roman'";
    ctx.globalAlpha = globalAlpha - 0.5;
    ctx.globalCompositeOperation = "source-out";
    ctx.fillStyle = "#eee";
    ctx.shadowColor = "blue";
    ctx.shadowOffsetX = 3;
    ctx.shadowOffsetY = -1;
    ctx.shadowBlur = shadowBlur + 0;
    ctx.fillText("blue shadow", 220, 100);
    ctx.restore();

    ctx.fillText("green shadow", 120, 140);
    ctx.restore();

    ctx.fillText("red shadow", 80, 180);
}
      </pre>
    </div>
  </body>
</html>
